﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace test_ttlang
{
    internal class AutoRun
    {
        public static void RunResult(ttlang.builder.ProjectResult result)
        {
            foreach (var rr in result.targets)
            {
                if (rr.autobuild != null)
                {
                    if (rr.target == ttlang.builder.BuildTarget.CSharp)
                    {
                        Console.WriteLine("==Run Csharp output:" + rr.autobuild);
                        var path = System.IO.Path.GetDirectoryName(rr.autobuild);
                        while (path[0] == ' ')
                            path = path.Substring(1);
                        ttlang.builder.SystemCall.ClearEnvVar();
                        ttlang.builder.SystemCall.SetWorkingPath(path);
                        ttlang.builder.SystemCall.Call("dotnet " + rr.autobuild,
                            (txt) =>
                          {
                              System.Console.WriteLine("recv:" + txt);
                          }, (txt) =>
                          {
                              System.Console.WriteLine("recv err:" + txt);
                          }
                          );
                    }
                    if (rr.target == ttlang.builder.BuildTarget.TypeScript)
                    {
                        Console.WriteLine("==Run TypeScript output:" + rr.autobuild);
                        var path = System.IO.Path.GetDirectoryName(rr.autobuild);
                        while (path[0] == ' ')
                            path = path.Substring(1);
                        ttlang.builder.SystemCall.ClearEnvVar();
                        ttlang.builder.SystemCall.SetWorkingPath(path);
                        ttlang.builder.SystemCall.Call("node " + rr.autobuild,
                            (txt) =>
                            {
                                System.Console.WriteLine("recv:" + txt);
                            }, (txt) =>
                            {
                                System.Console.WriteLine("recv err:" + txt);
                            }
                          );
                    }
                }
            }
        }
    }
}
